import redis from "../utils/redis.js";
const zsetName = "videohots";
// 视频热度推荐
export const videoHotInc = async (vId, type) => {
  const incNumMap = {
    watch: 1,
    comment: 2,
    like: 3,
    collect: 4,
  };
  const incNum = incNumMap[type];
  const data = await redis.zscore(zsetName, vId);
  let zset;
  if (data) {
    zset = await redis.zincrby(zsetName, incNum, vId);
  } else {
    zset = await redis.zadd(zsetName, incNum, vId);
  }
  return zset;
};
// 获取视频热度推荐
export const getVideoHots = async (num) => {
  const data = await redis.zrevrange(zsetName, 0, -1, "withscores");
  const newData = data.splice(0, num * 2);
  let result = {};
  for (let i = 0; i < newData.length; i++) {
    if (i % 2 === 0) {
      result[newData[i]] = newData[i + 1];
    }
  }
  return result;
};
